package com.xoa.controller.inv;

import com.xoa.dao.crm.CustomerMapper;
import com.xoa.model.crm.Customer;
import com.xoa.model.crm.CustomerWithBLOBs;
import com.xoa.model.inv.OmShipment;
import com.xoa.model.inv.Repayment;
import com.xoa.model.inv.SupRepayment;
import com.xoa.service.inv.OmShipmentService;
import com.xoa.service.inv.RepaymentService;
import com.xoa.util.ToJson;
import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 客户还款
 */
@Controller
@RequestMapping("/repayment")
public class RepaymentController {
    @Autowired
    private RepaymentService repaymentService;
    @Autowired
    private OmShipmentService omShipmentService;

    //处理时间  将前台传过来的时间存到数据库中
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        sdf.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }

    /**
     * 查询所有客户还款记录
     */
    @RequestMapping("/selectCusPayment")
    @ResponseBody
    public ToJson<Repayment> selectAll(Integer page, Repayment repayment, Integer pageSize, boolean useFlag, Integer type) {
        ToJson json = new ToJson<Repayment>(1, "err");
        Map<String, Object> map = new HashMap<String, Object>();
        PageParams pageParams = new PageParams();
        pageParams.setUseFlag(useFlag);
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        map.put("type", type);
        map.put("record", repayment);
        map.put("page", pageParams);
        try {
            List<Repayment> repayments = repaymentService.selectCusPayment(map);
            if (repayments != null) {
                json.setTotleNum(pageParams.getTotal());
                json.setObj(repayments);
                json.setFlag(0);
                json.setMsg("true");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 根据主键id删除客户还款记录
     */
    @RequestMapping("/delCusPaymentById")
    @ResponseBody
    public ToJson<Integer> deleteByPrimaryKey(Integer repaymentId) {
        ToJson json = new ToJson<Integer>(1, "err");
        try {
            Integer data = repaymentService.delCusPaymentById(repaymentId);
            if (data != null) {
                json.setObject(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 添加客户还款数据
     */
    @RequestMapping("/insertCusPayment")
    @ResponseBody
    public ToJson<Integer> insertSales(Repayment repayment) {
        ToJson json = new ToJson<Integer>(1, "err");
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            Integer data = repaymentService.insertCusPayment(repayment);
            if (data != null) {
                json.setObject(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 查询欠款的客户
     */
    @RequestMapping("/selectCustomerId")
    @ResponseBody
    public ToJson<Repayment> selectCustomerId(String customerName) {
        ToJson json = new ToJson<Repayment>(1, "err");
        try {
            List<OmShipment> omShipments = omShipmentService.selectCustomerId(customerName);
            List<OmShipment> news = new ArrayList<>();
            if (customerName != null && customerName != "") {
                for (OmShipment omShipment : omShipments
                ) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("obj", omShipment);
                    map.put("isArrears", 1);
                    List<OmShipment> shipments = omShipmentService.selectAll(map);
                    for (OmShipment omShipment1 : shipments
                    ) {
                        news.add(omShipment1);
                    }

                }
                json.setObj(news);
                json.setFlag(0);
                json.setMsg("true");
            } else {
                json.setObj(omShipments);
                json.setFlag(0);
                json.setMsg("true");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 查询所有供应商还款记录
     */
    @RequestMapping("/selectSupPayment")
    @ResponseBody
    public ToJson<Repayment> selectSupPayment(Integer page, SupRepayment supRepayment, Integer pageSize, boolean useFlag, Integer type) {
        ToJson json = new ToJson<Repayment>(1, "err");
        Map<String, Object> map = new HashMap<String, Object>();
        PageParams pageParams = new PageParams();
        pageParams.setUseFlag(useFlag);
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        map.put("type", type);
        map.put("record", supRepayment);
        map.put("page", pageParams);
        try {
            List<SupRepayment> list = repaymentService.selectSupPayment(map);
            if (list != null) {
                json.setTotleNum(pageParams.getTotal());
                json.setObj(list);
                json.setFlag(0);
                json.setMsg("true");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 根据主键id删除客户还款记录
     */
    @RequestMapping("/delSupPaymentById")
    @ResponseBody
    public ToJson<Integer> delSupPaymentById(Integer supRepaymentId) {
        ToJson json = new ToJson<Integer>(1, "err");
        try {
            Integer data = repaymentService.delSupPaymentById(supRepaymentId);
            if (data != null) {
                json.setObject(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 添加供应商还款数据
     */
    @RequestMapping("/insertSupPayment")
    @ResponseBody
    public ToJson<Integer> insertSupPayment(SupRepayment supRepayment) {
        ToJson json = new ToJson<Integer>(1, "err");
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            Integer data = repaymentService.insertSupPayment(supRepayment);
            if (data != null) {
                json.setObject(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
}
